/**
 * Copyright (c) 2010, iDA MediaFoundry
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * - Neither the name of iDA MediaFoundry nor the names of its contributors may
 *   be used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package be.idamf.flash.filesystem
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.ObjectEncoding;
	import flash.utils.ByteArray;

	/**
	 * Utility class related to loading/saving data.
	 * 
	 * @author Jan Van Coppenolle
	 */
	public class FileStorage
	{
		/**
		 * Loads the first (or only) Object from a file.
		 */
		public static function loadObject(file:File):Object
		{
			var stream:FileStream = new FileStream();
			stream.objectEncoding = ObjectEncoding.AMF3;
			stream.open(file, FileMode.READ);
			var object:Object = stream.readObject();
			stream.close();
			return object;
		}

		/**
		 * Loads all Objects from a file.
		 */
		public static function loadObjects(file:File):Array
		{
			var array:Array = [];
			var stream:FileStream = new FileStream();
			stream.objectEncoding = ObjectEncoding.AMF3;
			stream.open(file, FileMode.READ);
			while (stream.bytesAvailable > 0)
				array.push(stream.readObject());
			stream.close();
			return array;
		}
		
		/**
		 * Loads a ByteArray from a file.
		 */
		public static function loadBytes(file:File, offset:uint = 0, length:uint = 0):ByteArray
		{
			var bytes:ByteArray = new ByteArray();
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.READ);
			stream.readBytes(bytes, offset, length);
			stream.close();
			return bytes;
		}

		/**
		 * Saves an Object to a file.
		 */
		public static function saveObject(file:File, object:Object):void
		{
			saveObjects(file, [object]);
		}

		/**
		 * Saves Objects to a file.
		 */
		public static function saveObjects(file:File, objects:Array):void
		{
			var stream:FileStream = new FileStream();
			stream.objectEncoding = ObjectEncoding.AMF3;
			stream.open(file, FileMode.WRITE);
			for each (var object:Object in objects)
				stream.writeObject(object);
			stream.close();
		}

		/**
		 * Saves a ByteArray to a file.
		 */
		public static function saveBytes(file:File, bytes:ByteArray, offset:uint = 0, length:uint = 0):void
		{
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.WRITE);
			stream.writeBytes(bytes, offset, length);
			stream.close();
		}
	}
}